package com.sihan.swvp.service.video;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sihan.swvp.dao.jc.JcBzjsDao;
import com.sihan.swvp.dao.sys.VideoAutoMatchDao;
import com.sihan.swvp.dao.video.DjdDao;
import com.sihan.swvp.dao.video.VideoMatchDao;
import com.sihan.swvp.entity.jc.JcBzjs;
import com.sihan.swvp.entity.video.VideoManage;

@Service
public class VideoMatchService {
	
	@Autowired
	private VideoMatchDao videoMatchDao;
	
	@Autowired
	VideoAutoMatchDao matchDao;
	
	@Autowired
	private DjdDao djdDao;
	@Autowired
	private JcBzjsDao bzjsDao;
	
	public Page<Map<String, Object>> getTaskList(Map<String, Object> kv,Integer page,Integer limit){
		PageHelper.startPage(page, limit);
		Page<Map<String, Object>> list = videoMatchDao.getTaskList(kv);
		return list;
	}

	public List<Map<String, Object>> getSynPath(Map<String, Object> kv,Integer page,Integer limit){
		
		return videoMatchDao.getSynPath(kv);
	}
	
	public List<Map<String, Object>> getSynVideo(Map<String, Object> kv){
		
		return videoMatchDao.getSynVideo(kv);
	}
	
	public List<Map<String, Object>> getJCandgcVideo(String taskId){
		return videoMatchDao.getJCandgcVideo(taskId);
	}
	
	public List<Map<String, Object>> getGc(String rwid){
		return videoMatchDao.getGc(rwid);
	}
	
	public void addVideoManage(VideoManage vm,String mark,Integer delId,Integer taskType){
		if("update".equals(mark)){
			/*此处删除了video_manage表的关联数据，
			同时更新了video_syn表的match字段为0（未关联）状态*/
			if(null != delId){
				videoMatchDao.deleteVSyn(delId);
			}
			/*当为修改时，要删除更新已关联的数据，*/
			if(vm.getVideo_id() > 0){
				videoMatchDao.deleteVSyn(vm.getVideo_id());
			}
		}
		
		videoMatchDao.addVideoManage(vm);//添加关联数据
		
		matchDao.taskComplete(vm.getTask_id());
		//修改同步视频为关联状态
		videoMatchDao.updataVMstatus(vm.getVideo_id());
	}
	
	public int getVideoByvid(int videoId,String taskId){
		return videoMatchDao.getVideoByvid(videoId,taskId);
	}
	
	public int getStageByvid(VideoManage vm){
		return videoMatchDao.getStageByvid(vm);
	}
	
	public List<Map<String, Object>> getVideoByTaskid(String taskId,Integer roleId,Integer stageId){
		return videoMatchDao.getVideoByTaskid(taskId,roleId,stageId);
	}
	
	public List<Map<String, Object>> getBzRole(Integer bzId){
		return videoMatchDao.getBzRole(bzId);
	}
	
	public void deleteVSyn(int id){
		videoMatchDao.deleteVSyn(id);
	}
	
	public List<Map<String, Object>> getTaskManageV(String taskId,String js,String gc,String stage){
		String[] _js = null;String[] _gc = null;
		if(js != "" && js != null){
			_js = js.substring(0, js.length() - 1).split(",");
		}
		if(gc != "" && gc != null){
			_gc = gc.substring(0, gc.length() - 1).split(",");
		}
		
		return videoMatchDao.getTaskVideo(taskId,_js,_gc,stage);
	}
	
	public Object getVideoDetail(String taskId,String[] roles,String[] gcs,String bzid,int taskType){
		if( null == roles) {
			List<Map<String ,Object>> rolelist = djdDao.getJs(taskId,taskType);
			roles = new String[rolelist.size()];
			for (int i = 0; i < rolelist.size(); i++) {
				roles[i] = rolelist.get(i).get("jsid").toString();
			}
		}
		if( null == gcs) {
			List<Map<String ,Object>> gclist = djdDao.getGcs(taskId);
			gcs = new String[gclist.size()];
			for (int i = 0; i < gclist.size(); i++) {
				gcs[i] = gclist.get(i).get("id").toString();
			}
		}
		
		
		List<Object> listvideo = new ArrayList<Object>();
		
		for (String roleid : roles) {
			for (String gcid : gcs) {
				if(videoMatchDao.isVideoManageData(taskId, roleid, gcid) < 1) {
					continue;
				}
				List<Map<String ,Object>> gcvideos = djdDao.getGcVideos(taskId,roleid,gcid);
				Map<String,Object> map = new HashMap<>();
				map.put("videos", gcvideos);
				map.put("role", bzjsDao.getJcBzjsBean(roleid));
				map.put("gc", bzjsDao.getBzzyZygcBean(gcid));
				listvideo.add(map);
			}
		}
		
	return listvideo;	
	}
}
